package com.liuyh.demo.shardingjdbctest.controller;

import com.liuyh.demo.shardingjdbctest.entity.Order;
import com.liuyh.demo.shardingjdbctest.mybatis.repository.MybatisOrderRepository;
import com.liuyh.demo.shardingjdbctest.service.ExampleService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderController {
    @Resource(name = "order")
    ExampleService es;

    @Resource
    MybatisOrderRepository mor;
    @PostMapping("/init")
    public String init(){
        try {
            es.initEnvironment();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return throwables.getMessage();
        }
        return "success!";
    }

    @PostMapping("/processSuccess")
    public String processSuccess(){
        try {
            es.processSuccess();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return throwables.getMessage();
        }
        return "success!";
    }

    @PostMapping("/processFailure")
    public String processFailure(){
        try {
            es.processFailure();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return throwables.getMessage();
        }
        return "success!";
    }
    @PostMapping("selectAll")
    public List selectAll(){
        List<Order> lstOrder = null;
        try {
            lstOrder = mor.selectAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return lstOrder;
    }

    @PostMapping("selectByStatus")
    public List selectByStatus(@RequestBody Map input){
        List<Order> lstOrder = mor.selectByStatus((String) input.get("status"));
        return lstOrder;
    }

    @PostMapping("selectJoin")
    public List selectJoin(){
        List<Order> lstOrder = mor.selectJoin();
        return lstOrder;
    }
}
